function crearRequerimiento(){
	var pagina_requerida = false;
	if (window.XMLHttpRequest){
        // Si es Mozilla, Safari etc
        pagina_requerida = new XMLHttpRequest();		
    }else if (window.ActiveXObject){
        // pero si es IE
        try {
            pagina_requerida = new ActiveXObject ("Msxml2.XMLHTTP");
        }catch (e){
            // en caso que sea una versi�n antigua
            try{
                pagina_requerida = new ActiveXObject ("Microsoft.XMLHTTP");
            }catch (e){
            }
        }
    } 
	return pagina_requerida;
}

var menuXML;
function consultarMenu(){
	var req=crearRequerimiento();
    req.open("GET","/DenunciaDelitos/menu/prob.jsp");
    req.onreadystatechange=function(){
        if(req.readyState==4){
            if (req.status==200){
                menuXML = req.responseXML;
                crearMenu();
            }
        }
    }
    req.send(null);
}

function crearMenu(){
    var ul = document.createElement("ul");
    ul.setAttribute("id", "lista");

    var refMenu = menuXML.getElementsByTagName("menu");
    
    for(i=0; i<refMenu.length;i++){
        var refNombre = refMenu[i].getElementsByTagName("nombre");
        var refOpciones = refMenu[i].getElementsByTagName("opcion");

        var a = document.createElement("a");
        

        var txt = document.createTextNode(refNombre[0].firstChild.nodeValue);
        a.appendChild(txt);

        var li = document.createElement("li");
        li.setAttribute("value", i);
        li.setAttribute("onMouseOver", "javascript: showMenu(this);");
        //li.setAttribute("onMouseOut" , "javascript: hideMenu();");
        li.appendChild(a);

        ul.appendChild(li);
    }
    var divmenu = document.getElementById("menu");
    var ulant = document.getElementById("lista");
    if(ulant != undefined){
        divmenu.replaceChild(ul, ulant);
    }else{
        divmenu.appendChild(ul);
    }
}

function showMenu(menu){
    var divnew = document.createElement("div");
    divnew.setAttribute("id","submenu");

    var ul = document.createElement("ul");
    //ul.setAttribute("id", "submenu");

    var refMenu = menuXML.getElementsByTagName("menu");
    var refOpciones = refMenu[menu.value].getElementsByTagName("opcion");

    for(i=0; i<refOpciones.length;i++){
        var a = document.createElement("a");
        a.setAttribute("href", refOpciones[i].firstChild.nodeValue);
        
        var txt = document.createTextNode(refOpciones[i].firstChild.nodeValue);
        a.appendChild(txt);

        var li = document.createElement("li");
        li.appendChild(a);

        ul.appendChild(li);
    }

    divnew.appendChild(ul);
    var divcontenedor = document.getElementById("contenedor");
    var divant = document.getElementById("submenu");
    if(divant != undefined){
        divcontenedor.replaceChild(divnew, divant);
    }else{
        divcontenedor.appendChild(divnew);
    }
}

function hideMenu(){
    var divcontenedor = document.getElementById("contenedor");
    var divsubmenu = document.getElementById("submenu");
    if(divsubmenu != undefined){
        divcontenedor.removeChild(divsubmenu);
    }
}

function subMenuEntered(evento){
    var ref = evento.source;
    var divsubmenu = document.getElementById("submenu");
    if(divsubmenu == undefined) return;
    if(ref != divsubmenu){
        hideMenu();
    }
}

